package com.yunkeji.common.core.domain.event;

import cn.hutool.json.JSONObject;
import com.yunkeji.common.exception.AppServiceException;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import java.io.Serializable;
import java.util.Arrays;
import java.util.Optional;

/**
 * 跑马灯推送
 *
 * @author Lion Li
 */
@Data
@Slf4j
@Builder
public class PushRollingEvent implements Serializable {
    private Integer type;
    private String headUrl;
    private String nickName;
    private JSONObject content;

    /**
     * 跑马灯类型
     *
     * @author yunkeji
     */
    @Getter
    @AllArgsConstructor
    public enum Type {
        会员(1),
        充值(2),
        礼物(3);
        private Integer code;

        public Type getTypeByCode(Integer code) {
            Type[] values = Type.values();
            Optional<Type> first =
                    Arrays.stream(values).filter(item -> item.getCode().equals(code)).findFirst();
            if (first.isPresent()) {
                return first.get();
            } else {
                throw AppServiceException.paramException("跑马灯类型参数异常");
            }
        }
    }
}
